/*
 * Function0.c
 *
 * Code generation for model "Function0".
 *
 * Model version              : 5.440
 * Simulink Coder version : 9.7 (R2022a) 13-Nov-2021
 * C source code generated on : Tue May 16 10:41:55 2023
 *
 * Target selection: grt.tlc
 * Note: GRT includes extra infrastructure and instrumentation for prototyping
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Function0.h"
#include <math.h>
#include "rtwtypes.h"
#include <string.h>
#include "Function0_private.h"
#include "rt_nonfinite.h"

/* External inputs (root inport signals with default storage) */
ExtU_Function0_T Function0_U;

/* External outputs (root outports fed by signals with default storage) */
ExtY_Function0_T Function0_Y;

/* Real-time model */
static RT_MODEL_Function0_T Function0_M_;
RT_MODEL_Function0_T *const Function0_M = &Function0_M_;

/* Model step function */
void Function0_step(void)
{
  real_T tmp[20];
  real_T rtb_Addminy;
  real_T rtb_DotProduct;
  real_T tmp_0;
  real_T tmp_1;
  real_T tmp_2;
  real_T tmp_3;
  real_T tmp_4;
  real_T tmp_5;
  real_T tmp_6;
  real_T tmp_7;
  real_T tmp_8;
  real_T tmp_9;
  real_T tmp_a;
  real_T tmp_b;
  real_T tmp_c;
  real_T tmp_d;
  real_T tmp_e;
  real_T tmp_f;
  real_T tmp_g;
  real_T tmp_h;
  real_T tmp_i;
  real_T tmp_j;
  int32_T i;

  /* Gain: '<S33>/range y // range x' */
  rtb_DotProduct = Function0_P.mapminmax_ymax - Function0_P.mapminmax_ymin;

  /* Bias: '<S33>/Add min y' incorporates:
   *  Bias: '<S33>/Subtract min x'
   *  Gain: '<S33>/range y // range x'
   *  Inport: '<Root>/Input'
   */
  rtb_Addminy = rtb_DotProduct / (Function0_P.mapminmax_xmax[0] -
    Function0_P.mapminmax_xmin[0]) * (Function0_U.input[0] +
    -Function0_P.mapminmax_xmin[0]) + Function0_P.mapminmax_ymin;

  /* DotProduct: '<S9>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(1,:)''
   */
  tmp_0 = Function0_P.IW111_Value[0] * rtb_Addminy;

  /* DotProduct: '<S20>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(2,:)''
   */
  tmp_1 = Function0_P.IW112_Value[0] * rtb_Addminy;

  /* DotProduct: '<S22>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(3,:)''
   */
  tmp_2 = Function0_P.IW113_Value[0] * rtb_Addminy;

  /* DotProduct: '<S23>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(4,:)''
   */
  tmp_3 = Function0_P.IW114_Value[0] * rtb_Addminy;

  /* DotProduct: '<S24>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(5,:)''
   */
  tmp_4 = Function0_P.IW115_Value[0] * rtb_Addminy;

  /* DotProduct: '<S25>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(6,:)''
   */
  tmp_5 = Function0_P.IW116_Value[0] * rtb_Addminy;

  /* DotProduct: '<S26>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(7,:)''
   */
  tmp_6 = Function0_P.IW117_Value[0] * rtb_Addminy;

  /* DotProduct: '<S27>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(8,:)''
   */
  tmp_7 = Function0_P.IW118_Value[0] * rtb_Addminy;

  /* DotProduct: '<S28>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(9,:)''
   */
  tmp_8 = Function0_P.IW119_Value[0] * rtb_Addminy;

  /* DotProduct: '<S10>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(10,:)''
   */
  tmp_9 = Function0_P.IW1110_Value[0] * rtb_Addminy;

  /* DotProduct: '<S11>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(11,:)''
   */
  tmp_a = Function0_P.IW1111_Value[0] * rtb_Addminy;

  /* DotProduct: '<S12>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(12,:)''
   */
  tmp_b = Function0_P.IW1112_Value[0] * rtb_Addminy;

  /* DotProduct: '<S13>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(13,:)''
   */
  tmp_c = Function0_P.IW1113_Value[0] * rtb_Addminy;

  /* DotProduct: '<S14>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(14,:)''
   */
  tmp_d = Function0_P.IW1114_Value[0] * rtb_Addminy;

  /* DotProduct: '<S15>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(15,:)''
   */
  tmp_e = Function0_P.IW1115_Value[0] * rtb_Addminy;

  /* DotProduct: '<S16>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(16,:)''
   */
  tmp_f = Function0_P.IW1116_Value[0] * rtb_Addminy;

  /* DotProduct: '<S17>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(17,:)''
   */
  tmp_g = Function0_P.IW1117_Value[0] * rtb_Addminy;

  /* DotProduct: '<S18>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(18,:)''
   */
  tmp_h = Function0_P.IW1118_Value[0] * rtb_Addminy;

  /* DotProduct: '<S19>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(19,:)''
   */
  tmp_i = Function0_P.IW1119_Value[0] * rtb_Addminy;

  /* DotProduct: '<S21>/Dot Product' incorporates:
   *  Constant: '<S7>/IW{1,1}(20,:)''
   */
  tmp_j = Function0_P.IW1120_Value[0] * rtb_Addminy;

  /* Bias: '<S33>/Add min y' incorporates:
   *  Bias: '<S33>/Subtract min x'
   *  Gain: '<S33>/range y // range x'
   *  Inport: '<Root>/Input'
   */
  rtb_Addminy = rtb_DotProduct / (Function0_P.mapminmax_xmax[1] -
    Function0_P.mapminmax_xmin[1]) * (Function0_U.input[1] +
    -Function0_P.mapminmax_xmin[1]) + Function0_P.mapminmax_ymin;

  /* Sum: '<S2>/netsum' incorporates:
   *  Constant: '<S2>/b{1}'
   *  Constant: '<S7>/IW{1,1}(1,:)''
   *  Constant: '<S7>/IW{1,1}(10,:)''
   *  Constant: '<S7>/IW{1,1}(11,:)''
   *  Constant: '<S7>/IW{1,1}(12,:)''
   *  Constant: '<S7>/IW{1,1}(13,:)''
   *  Constant: '<S7>/IW{1,1}(14,:)''
   *  Constant: '<S7>/IW{1,1}(15,:)''
   *  Constant: '<S7>/IW{1,1}(16,:)''
   *  Constant: '<S7>/IW{1,1}(17,:)''
   *  Constant: '<S7>/IW{1,1}(18,:)''
   *  Constant: '<S7>/IW{1,1}(19,:)''
   *  Constant: '<S7>/IW{1,1}(2,:)''
   *  Constant: '<S7>/IW{1,1}(20,:)''
   *  Constant: '<S7>/IW{1,1}(3,:)''
   *  Constant: '<S7>/IW{1,1}(4,:)''
   *  Constant: '<S7>/IW{1,1}(5,:)''
   *  Constant: '<S7>/IW{1,1}(6,:)''
   *  Constant: '<S7>/IW{1,1}(7,:)''
   *  Constant: '<S7>/IW{1,1}(8,:)''
   *  Constant: '<S7>/IW{1,1}(9,:)''
   *  DotProduct: '<S10>/Dot Product'
   *  DotProduct: '<S11>/Dot Product'
   *  DotProduct: '<S12>/Dot Product'
   *  DotProduct: '<S13>/Dot Product'
   *  DotProduct: '<S14>/Dot Product'
   *  DotProduct: '<S15>/Dot Product'
   *  DotProduct: '<S16>/Dot Product'
   *  DotProduct: '<S17>/Dot Product'
   *  DotProduct: '<S18>/Dot Product'
   *  DotProduct: '<S19>/Dot Product'
   *  DotProduct: '<S20>/Dot Product'
   *  DotProduct: '<S21>/Dot Product'
   *  DotProduct: '<S22>/Dot Product'
   *  DotProduct: '<S23>/Dot Product'
   *  DotProduct: '<S24>/Dot Product'
   *  DotProduct: '<S25>/Dot Product'
   *  DotProduct: '<S26>/Dot Product'
   *  DotProduct: '<S27>/Dot Product'
   *  DotProduct: '<S28>/Dot Product'
   *  DotProduct: '<S9>/Dot Product'
   *  Gain: '<S8>/Gain'
   */
  tmp[0] = ((Function0_P.IW111_Value[1] * rtb_Addminy + tmp_0) +
            Function0_P.b1_Value[0]) * Function0_P.Gain_Gain;
  tmp[1] = ((Function0_P.IW112_Value[1] * rtb_Addminy + tmp_1) +
            Function0_P.b1_Value[1]) * Function0_P.Gain_Gain;
  tmp[2] = ((Function0_P.IW113_Value[1] * rtb_Addminy + tmp_2) +
            Function0_P.b1_Value[2]) * Function0_P.Gain_Gain;
  tmp[3] = ((Function0_P.IW114_Value[1] * rtb_Addminy + tmp_3) +
            Function0_P.b1_Value[3]) * Function0_P.Gain_Gain;
  tmp[4] = ((Function0_P.IW115_Value[1] * rtb_Addminy + tmp_4) +
            Function0_P.b1_Value[4]) * Function0_P.Gain_Gain;
  tmp[5] = ((Function0_P.IW116_Value[1] * rtb_Addminy + tmp_5) +
            Function0_P.b1_Value[5]) * Function0_P.Gain_Gain;
  tmp[6] = ((Function0_P.IW117_Value[1] * rtb_Addminy + tmp_6) +
            Function0_P.b1_Value[6]) * Function0_P.Gain_Gain;
  tmp[7] = ((Function0_P.IW118_Value[1] * rtb_Addminy + tmp_7) +
            Function0_P.b1_Value[7]) * Function0_P.Gain_Gain;
  tmp[8] = ((Function0_P.IW119_Value[1] * rtb_Addminy + tmp_8) +
            Function0_P.b1_Value[8]) * Function0_P.Gain_Gain;
  tmp[9] = ((Function0_P.IW1110_Value[1] * rtb_Addminy + tmp_9) +
            Function0_P.b1_Value[9]) * Function0_P.Gain_Gain;
  tmp[10] = ((Function0_P.IW1111_Value[1] * rtb_Addminy + tmp_a) +
             Function0_P.b1_Value[10]) * Function0_P.Gain_Gain;
  tmp[11] = ((Function0_P.IW1112_Value[1] * rtb_Addminy + tmp_b) +
             Function0_P.b1_Value[11]) * Function0_P.Gain_Gain;
  tmp[12] = ((Function0_P.IW1113_Value[1] * rtb_Addminy + tmp_c) +
             Function0_P.b1_Value[12]) * Function0_P.Gain_Gain;
  tmp[13] = ((Function0_P.IW1114_Value[1] * rtb_Addminy + tmp_d) +
             Function0_P.b1_Value[13]) * Function0_P.Gain_Gain;
  tmp[14] = ((Function0_P.IW1115_Value[1] * rtb_Addminy + tmp_e) +
             Function0_P.b1_Value[14]) * Function0_P.Gain_Gain;
  tmp[15] = ((Function0_P.IW1116_Value[1] * rtb_Addminy + tmp_f) +
             Function0_P.b1_Value[15]) * Function0_P.Gain_Gain;
  tmp[16] = ((Function0_P.IW1117_Value[1] * rtb_Addminy + tmp_g) +
             Function0_P.b1_Value[16]) * Function0_P.Gain_Gain;
  tmp[17] = ((Function0_P.IW1118_Value[1] * rtb_Addminy + tmp_h) +
             Function0_P.b1_Value[17]) * Function0_P.Gain_Gain;
  tmp[18] = ((Function0_P.IW1119_Value[1] * rtb_Addminy + tmp_i) +
             Function0_P.b1_Value[18]) * Function0_P.Gain_Gain;
  tmp[19] = ((Function0_P.IW1120_Value[1] * rtb_Addminy + tmp_j) +
             Function0_P.b1_Value[19]) * Function0_P.Gain_Gain;

  /* DotProduct: '<S32>/Dot Product' incorporates:
   *  Constant: '<S30>/IW{2,1}(1,:)''
   *  Constant: '<S8>/one'
   *  Constant: '<S8>/one1'
   *  Gain: '<S8>/Gain1'
   *  Math: '<S8>/Exp'
   *  Math: '<S8>/Reciprocal'
   *  Sum: '<S8>/Sum'
   *  Sum: '<S8>/Sum1'
   *
   * About '<S8>/Exp':
   *  Operator: exp
   *
   * About '<S8>/Reciprocal':
   *  Operator: reciprocal
   */
  tmp_0 = 0.0;
  for (i = 0; i < 20; i++) {
    tmp_0 += (1.0 / (exp(tmp[i]) + Function0_P.one_Value) *
              Function0_P.Gain1_Gain - Function0_P.one1_Value) *
      Function0_P.IW211_Value[i];
  }

  /* Outport: '<Root>/Output' incorporates:
   *  Bias: '<S34>/Add min x'
   *  Bias: '<S34>/Subtract min y'
   *  Constant: '<S3>/b{2}'
   *  DotProduct: '<S32>/Dot Product'
   *  Gain: '<S34>/Divide by range y'
   *  Sum: '<S3>/netsum'
   */
  Function0_Y.Output = (Function0_P.mapminmax_reverse_xmax -
                        Function0_P.mapminmax_reverse_xmin) /
    (Function0_P.mapminmax_reverse_ymax - Function0_P.mapminmax_reverse_ymin) *
    ((tmp_0 + Function0_P.b2_Value) + -Function0_P.mapminmax_reverse_ymin) +
    Function0_P.mapminmax_reverse_xmin;

  /* Matfile logging */
  rt_UpdateTXYLogVars(Function0_M->rtwLogInfo, (&Function0_M->Timing.taskTime0));

  /* signal main to stop simulation */
  {                                    /* Sample time: [0.4s, 0.0s] */
    if ((rtmGetTFinal(Function0_M)!=-1) &&
        !((rtmGetTFinal(Function0_M)-Function0_M->Timing.taskTime0) >
          Function0_M->Timing.taskTime0 * (DBL_EPSILON))) {
      rtmSetErrorStatus(Function0_M, "Simulation finished");
    }
  }

  /* Update absolute time for base rate */
  /* The "clockTick0" counts the number of times the code of this task has
   * been executed. The absolute time is the multiplication of "clockTick0"
   * and "Timing.stepSize0". Size of "clockTick0" ensures timer will not
   * overflow during the application lifespan selected.
   * Timer of this task consists of two 32 bit unsigned integers.
   * The two integers represent the low bits Timing.clockTick0 and the high bits
   * Timing.clockTickH0. When the low bit overflows to 0, the high bits increment.
   */
  if (!(++Function0_M->Timing.clockTick0)) {
    ++Function0_M->Timing.clockTickH0;
  }

  Function0_M->Timing.taskTime0 = Function0_M->Timing.clockTick0 *
    Function0_M->Timing.stepSize0 + Function0_M->Timing.clockTickH0 *
    Function0_M->Timing.stepSize0 * 4294967296.0;
}

/* Model initialize function */
void Function0_initialize(void)
{
  /* Registration code */

  /* initialize non-finites */
  rt_InitInfAndNaN(sizeof(real_T));

  /* initialize real-time model */
  (void) memset((void *)Function0_M, 0,
                sizeof(RT_MODEL_Function0_T));
  rtmSetTFinal(Function0_M, 20.0);
  Function0_M->Timing.stepSize0 = 0.4;

  /* Setup for data logging */
  {
    static RTWLogInfo rt_DataLoggingInfo;
    rt_DataLoggingInfo.loggingInterval = (NULL);
    Function0_M->rtwLogInfo = &rt_DataLoggingInfo;
  }

  /* Setup for data logging */
  {
    rtliSetLogXSignalInfo(Function0_M->rtwLogInfo, (NULL));
    rtliSetLogXSignalPtrs(Function0_M->rtwLogInfo, (NULL));
    rtliSetLogT(Function0_M->rtwLogInfo, "tout");
    rtliSetLogX(Function0_M->rtwLogInfo, "");
    rtliSetLogXFinal(Function0_M->rtwLogInfo, "");
    rtliSetLogVarNameModifier(Function0_M->rtwLogInfo, "rt_");
    rtliSetLogFormat(Function0_M->rtwLogInfo, 4);
    rtliSetLogMaxRows(Function0_M->rtwLogInfo, 0);
    rtliSetLogDecimation(Function0_M->rtwLogInfo, 1);
    rtliSetLogY(Function0_M->rtwLogInfo, "");
    rtliSetLogYSignalInfo(Function0_M->rtwLogInfo, (NULL));
    rtliSetLogYSignalPtrs(Function0_M->rtwLogInfo, (NULL));
  }

  /* external inputs */
  (void)memset(&Function0_U, 0, sizeof(ExtU_Function0_T));

  /* external outputs */
  Function0_Y.Output = 0.0;

  /* Matfile logging */
  rt_StartDataLoggingWithStartTime(Function0_M->rtwLogInfo, 0.0, rtmGetTFinal
    (Function0_M), Function0_M->Timing.stepSize0, (&rtmGetErrorStatus
    (Function0_M)));
}

/* Model terminate function */
void Function0_terminate(void)
{
  /* (no terminate code required) */
}
